package com.hpjz.basicofing.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import lombok.extern.log4j.Log4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hpjz.basicofing.service.IUserService;
import com.hpjz.basicofing.web.vo.UserVO;
import com.hpjz.common.ParameterBean;
import com.hpjz.common.ResultBean;
import com.hpjz.common.utils.CodeUtils;
import com.hpjz.common.utils.ConstantDefinition;

@Controller
@RequestMapping("/login")
@Log4j
public class LoginController {
	
	@Resource
	private IUserService userService;
	
	
	@RequestMapping(value="/userLogin" ,method = RequestMethod.POST)
	@ResponseBody
	public ResultBean login(@RequestBody ParameterBean pb){
		
		UserVO objMap = (UserVO) pb.getParameter(UserVO.class);
		ResultBean result = new ResultBean();
		 try {
			 String pwd = objMap.getPwd();
			 if (StringUtils.isNotBlank(pwd)) {
				objMap.setPwd(CodeUtils.strToMD5(pwd));
			 }
			 
			 //用户名、密码、平台进行校验
			 String platform = pb.getPlatform();
			 objMap.setPlatform(platform);
			 
			 UserVO uvo =  this.userService.userLogin(objMap);
			 
			 System.out.println("返回的uvo 是："+uvo);
			 
			 String loginname = uvo.getLoginname();
			 
			 if (StringUtils.isNotBlank(loginname)) {
				 Map<String,String> map = new HashMap<>();
				 map.put("uid", uvo.getUid());
				 map.put("type",uvo.getType());
				 result.setResult(map);
				 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
			 }else{
				result.setStatus(ConstantDefinition.PWD_ERROR);
			}
			
			/*
			 if (StringUtils.isNotBlank(uvo.getPwd())&&(objMap.getPwd()).equals(uvo.getPwd())) {
			     	 //登陆成功
				  Map<String,String> map = new HashMap<>();
				 map.put("uid", uvo.getUid());
				 result.setResult(map);
				 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
			 }else{
				 result.setStatus(ConstantDefinition.PWD_ERROR);
			 }*/
			 
		} catch (Exception e) {
			 log.error("用户登录失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
		
	}
	
	@RequestMapping(value="/loginOut" ,method = RequestMethod.POST)
	@ResponseBody
	public ResultBean loginOut(@RequestBody ParameterBean pb){
		//修改登录信息
		UserVO objMap = (UserVO) pb.getParameter(UserVO.class);
		ResultBean result = new ResultBean();
		 try {
			 this.userService.userLoginOut(objMap);;
			 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
		} catch (Exception e) {
			 log.error("用户退出失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
		
	}
	

}
